package com.yyw.foodie.controller.base;


import com.yyw.foodie.bo.cart.ShopCartBo;
import com.yyw.foodie.common.Result;
import com.yyw.foodie.service.ItemsCommentsService;
import com.yyw.foodie.service.ItemsService;
import com.yyw.foodie.utils.PagedGridResult;
import com.yyw.foodie.vo.CommentLevelVo;
import com.yyw.foodie.vo.ItemsDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 前端控制器
 * </p>
 *
 * @author yiyw
 * @since 2021-05-22
 */
@RestController
@Api(value = "商品相关", tags = "商品详情页等相关接口")
@RequestMapping("/items")
@Slf4j
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @Autowired
    private ItemsCommentsService itemsCommentsService;

    @GetMapping("/info/{itemId}")
    @ApiOperation(value = "根据商品ID获取商品信息", notes = "商品详情页获取商品信息，包括商品图片、规格、sku信息等等")
    public Result getItemInfo(@ApiParam(value = "商品id", required = true) @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return Result.errorMsg("参数有误");
        }
        // 获取商品详情页所需要的信息
        try {
            ItemsDetailsVo itemsInfo = itemsService.getItemsInfo(itemId);
            return Result.ok(itemsInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.errorException(e.getMessage());
        }
    }


    @GetMapping("/commentLevel")
    @ApiOperation(value = "根据商品ID获取评价等级", notes = "根据商品信息获取评价等级(好评度)")
    public Result getCommentLevel(@ApiParam(value = "商品ID", required = true) @RequestParam("itemId") String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return Result.errorMsg("参数有误");
        }
        CommentLevelVo levelVo = itemsCommentsService.getCommentLevelByItemId(itemId);
        return Result.ok(levelVo);
    }


    @GetMapping("/comments")
    @ApiOperation(value = "分页获取评论信息", notes = "根据商品ID分页获取评论信息")
    public Result getCommentsByPage(@ApiParam(value = "商品ID", required = true) @RequestParam("itemId") String itemId,
                                    @ApiParam(value = "评价等级") @RequestParam(value = "level", required = false) Integer level,
                                    @ApiParam(value = "显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                                    @ApiParam(value = "当前页") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
        if (StringUtils.isBlank(itemId)) {
            return Result.errorMsg("参数有误");
        }
        PagedGridResult pageList = itemsCommentsService.getCommentPageByItemId(itemId, level, pageSize, page);
        return Result.ok(pageList);
    }


    @GetMapping("/search")
    @ApiOperation(value = "根据关键字商品搜索", notes = "根据关键字搜索商品分页展示")
    public Result getItemsByPage(@ApiParam(value = "关键字") @RequestParam(value = "keywords", required = false) String keyWords,
                                    @ApiParam(value = "排序规则(默认=k/销量=c/价格=p)") @RequestParam(value = "sort", required = false, defaultValue = "k") String sort,
                                    @ApiParam(value = "显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                    @ApiParam(value = "当前页") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
        PagedGridResult result = itemsService.itemsPage(keyWords, sort, page, pageSize);
        return Result.ok(result);
    }

    @GetMapping("/catItems")
    @ApiOperation(value = "根据分类商品搜索", notes = "侧边导航栏根据分类搜索商品")
    public Result getItemsByPageAndCategory(@ApiParam(value = "三级分类ID") @RequestParam(value = "catId", required = false) String catId,
                                 @ApiParam(value = "排序规则(默认=k/销量=c/价格=p)") @RequestParam(value = "sort", required = false, defaultValue = "k") String sort,
                                 @ApiParam(value = "显示条数") @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                 @ApiParam(value = "当前页") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
        try {
            Integer category = Integer.parseInt(catId);
        } catch (NumberFormatException e) {
            log.error("参数异常", e);
            return Result.errorMsg("请输入正确的参数");
        }
        PagedGridResult result = itemsService.itemsPageByCategory(catId, sort, page, pageSize);
        return Result.ok(result);
    }


    @GetMapping("/refresh")
    @ApiOperation(value = "获取最新的购物车信息", notes = "根据SkuId查询最新的商品信息")
    public Result getNewItemInfoByCart(@RequestParam("itemSpecIds") @ApiParam(value = "拼接后的skuId") String skuIds) {
        String[] skus = skuIds.split(",");
        List<ShopCartBo> list = itemsService.getItemsByCart(skus);
        return Result.ok(list);
    }
}

